
#include "../ui.h"
#include "../app_includes.h"

#define TIMER_PERIOD        (300)
#define SOLDER_TAB_NUM      (1)

static char *solder_type[] = {"未连接", "T12", "C245", "C210", "未知"};
static int32_t para_change_timeout = -1;


static lv_obj_t *ui_screen;
static lv_obj_t *ui_tabview;
static lv_obj_t *ui_tab[SOLDER_TAB_NUM];
static lv_timer_t *lv_timer;

static void solder_tab0_screen_create(lv_obj_t *tab);
static void solder_tab0_refresh(lv_timer_t *timer);
static void set_value_event_cb(lv_event_t * e);
static void power_select_refresh(void);
static void select_shortkey_event_cb(lv_event_t * e);
static void select_shortkey_back_event_cb(lv_event_t * e);

void lv_solder_close(void)
{
    lv_timer_del(lv_timer);
}

//app应用入口 
lv_obj_t *lv_solder_open(lv_obj_t *screen)
{
    ui_tabview = lv_tab_screen_create(screen, ui_tab, SOLDER_TAB_NUM);
    solder_tab0_screen_create(ui_tab[0]); 
    
    lv_timer = lv_timer_create(solder_tab0_refresh, TIMER_PERIOD, NULL);
    solder_group_select(0);
    
    return ui_tabview;
}


void panel_add_value_btns(lv_obj_t *panel, lv_obj_t *shortkey_btn[], lv_event_cb_t event_cb, uint16_t *val, uint8_t num)
{
    uint16_t i;
    lv_obj_t *btn_label;
    char str[10];

    for(i=0;i!=num;i++) {
        shortkey_btn[i] = lv_btn_create(panel);
        lv_obj_set_size(shortkey_btn[i], 60, 30);
        lv_obj_clear_flag( shortkey_btn[i], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
        lv_obj_set_style_bg_color(shortkey_btn[i], lv_color_hex(0x292831), LV_PART_MAIN | LV_STATE_DEFAULT );
        btn_label = lv_label_create(shortkey_btn[i]);
        lv_obj_set_style_text_font(btn_label, &ui_font_FontChineseSmall, LV_PART_MAIN | LV_STATE_DEFAULT);
        uint_to_str(str, val[i], 0, 0,"℃");
        lv_label_set_text(btn_label, str);
        lv_obj_align(btn_label, LV_ALIGN_CENTER, 0, 0);
        lv_obj_add_event_cb(shortkey_btn[i], event_cb, LV_EVENT_CLICKED, &val[i]);//添加事件
    }
}

#define SHORTKEY_NUM    (6)
static lv_obj_t *shortkey_btn[SHORTKEY_NUM];
static uint16_t shortkey_value[]= {100,200,300,330,380,420};

static lv_obj_t *ui_LabelSolderTemp;
static lv_obj_t *ui_LabelSolderSetTemp;
static lv_obj_t *ui_SliderSolderSet;
static lv_obj_t *ui_SliderSolderDuty;
static lv_obj_t *ui_LabelSolderDuty;
static lv_obj_t *ui_LabelCurrentVal;
static lv_obj_t *ui_LabelCurrentUnit;
static lv_obj_t *ui_PanelShortKey;
static lv_obj_t *ui_PanelSolderName;
static lv_obj_t *ui_LabelSolderName;

static void solder_tab0_screen_create(lv_obj_t *tab)
{
    ui_LabelSolderTemp = lv_label_create(tab);
    lv_obj_set_width( ui_LabelSolderTemp, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelSolderTemp, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( ui_LabelSolderTemp, LV_ALIGN_LEFT_MID, 0, -15);
    lv_label_set_text(ui_LabelSolderTemp,"379℃");
    lv_obj_set_style_text_font(ui_LabelSolderTemp, &ui_font_FontNumberBig, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_SliderSolderSet = lv_slider_create(tab);
    lv_slider_set_range(ui_SliderSolderSet, 0,45);
    lv_slider_set_value( ui_SliderSolderSet, 38, LV_ANIM_OFF);
    if (lv_slider_get_mode(ui_SliderSolderSet)==LV_SLIDER_MODE_RANGE ) lv_slider_set_left_value( ui_SliderSolderSet, 0, LV_ANIM_OFF);
    lv_obj_set_width( ui_SliderSolderSet, 80);
    lv_obj_set_height( ui_SliderSolderSet, 6);
    lv_obj_align( ui_SliderSolderSet, LV_ALIGN_RIGHT_MID, 0, -30 );

    lv_obj_set_style_bg_color(ui_SliderSolderSet, lv_color_hex(0xFFFFFF), LV_PART_KNOB | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_SliderSolderSet, 0, LV_PART_KNOB| LV_STATE_DEFAULT);

    ui_LabelSolderSetTemp = lv_label_create(tab);
    lv_obj_set_width( ui_LabelSolderSetTemp, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelSolderSetTemp, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelSolderSetTemp, ui_SliderSolderSet, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20 );
    lv_label_set_text(ui_LabelSolderSetTemp,"SET 380℃");
    lv_obj_set_style_text_font(ui_LabelSolderSetTemp, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_SliderSolderDuty = lv_slider_create(tab);
    lv_slider_set_range(ui_SliderSolderDuty, 0,100);
    lv_slider_set_value( ui_SliderSolderDuty, 50, LV_ANIM_OFF);
    if (lv_slider_get_mode(ui_SliderSolderDuty)==LV_SLIDER_MODE_RANGE ) lv_slider_set_left_value( ui_SliderSolderDuty, 0, LV_ANIM_OFF);
    lv_obj_set_width( ui_SliderSolderDuty, 60);
    lv_obj_set_height( ui_SliderSolderDuty, 20);
    lv_obj_align( ui_SliderSolderDuty, LV_ALIGN_RIGHT_MID, 0, -65);
    lv_obj_set_style_radius(ui_SliderSolderDuty, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    lv_obj_set_style_radius(ui_SliderSolderDuty, 0, LV_PART_INDICATOR| LV_STATE_DEFAULT);

    lv_obj_set_style_bg_color(ui_SliderSolderDuty, lv_color_hex(0xFF8080), LV_PART_KNOB | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_SliderSolderDuty, 0, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui_SliderSolderDuty, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_SliderSolderDuty, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(ui_SliderSolderDuty, 2, LV_PART_KNOB| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui_SliderSolderDuty, 2, LV_PART_KNOB| LV_STATE_DEFAULT);

    ui_LabelSolderDuty = lv_label_create(ui_SliderSolderDuty);
    lv_obj_set_width( ui_LabelSolderDuty, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelSolderDuty, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelSolderDuty, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelSolderDuty,"50%");
    lv_obj_set_style_text_color(ui_LabelSolderDuty, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelSolderDuty, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelSolderDuty, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);


    ui_PanelShortKey = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelShortKey, 220);
    lv_obj_set_height( ui_PanelShortKey, 90);
    lv_obj_align( ui_PanelShortKey, LV_ALIGN_BOTTOM_MID, 0, 10);
    lv_obj_clear_flag( ui_PanelShortKey, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelShortKey, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelShortKey, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_side(ui_PanelShortKey, LV_BORDER_SIDE_TOP, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui_PanelShortKey, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_PanelShortKey, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelSolderName = lv_obj_create(tab);
    lv_obj_set_width( ui_PanelSolderName, 150);
    lv_obj_set_height( ui_PanelSolderName, 22);
    lv_obj_align( ui_PanelSolderName, LV_ALIGN_LEFT_MID, 0, -65);
    lv_obj_clear_flag( ui_PanelSolderName, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelSolderName, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelSolderName, lv_color_hex(0x103041), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelSolderName, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelSolderName, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelSolderName = lv_label_create(ui_PanelSolderName);
    lv_obj_align( ui_LabelSolderName, LV_ALIGN_LEFT_MID, 0, 0);
    //lv_label_set_text(ui_LabelSolderName,"烙铁芯: 未插入");
    lv_obj_set_style_text_font(ui_LabelSolderName, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelSolderName, 10, LV_PART_MAIN| LV_STATE_DEFAULT);

    lv_obj_set_flex_flow(ui_PanelShortKey, LV_FLEX_FLOW_ROW_WRAP);
    lv_obj_set_flex_align(ui_PanelShortKey, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
    lv_obj_set_flex_grow(ui_PanelShortKey,20);
    panel_add_value_btns(ui_PanelShortKey, shortkey_btn, select_shortkey_back_event_cb, shortkey_value, 6);
    
    
    lv_obj_add_event_cb(ui_SliderSolderSet, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

static void solder_tab0_refresh(lv_timer_t *timer)
{
    char str[32];
    uint8_t duty = 0;
    strcpy(str, "烙铁芯: ");
    strcat(str, solder_type[solder.flag.type]);
    lv_label_set_text(ui_LabelSolderName, str);
    
    strcpy(str, "SET ");
    uint_to_str(str+strlen(str), app_dev.para.run.solder_temp, 0, 3, "℃");
    lv_label_set_text(ui_LabelSolderSetTemp, str);
    
    if(solder.flag.type==kSolderType_Disconnect)
        strcpy(str, "---℃");
    else
        uint_to_str(str, (uint32_t)app_dev.meter.solder_temp.value, 0, 3, "℃"); //刷新温度
    lv_label_set_text(ui_LabelSolderTemp, str);
    
    if(solder.flag.work==kDeviceWork_Enable)
        lv_obj_set_style_text_color(ui_LabelSolderTemp, lv_color_hex(0xffffff), LV_PART_MAIN | LV_STATE_DEFAULT );
    else if(solder.flag.work==kDeviceWork_Sleep)
        lv_obj_set_style_text_color(ui_LabelSolderTemp, lv_color_hex(0x41D080), LV_PART_MAIN | LV_STATE_DEFAULT );
    else
        lv_obj_set_style_text_color(ui_LabelSolderTemp, lv_color_hex(0x00FF80), LV_PART_MAIN | LV_STATE_DEFAULT );
        
    duty = solder.d.val/10;
    uint_to_str(str, duty, 0, 3, "%");//刷新占空比
    lv_label_set_text(ui_LabelSolderDuty, str);
    lv_slider_set_value( ui_SliderSolderDuty, duty, LV_ANIM_ON);
    
    //refresh para
    EventBits_t event = xEventGroupWaitBits(sys_event,kSysEvent_UIParaChange,pdTRUE,pdFALSE,0);
    if(event & kSysEvent_UIParaChange) {
        if(app_dev.para.run.solder_temp!=lv_slider_get_value(ui_SliderSolderSet)) {
            lv_obj_remove_event_cb(ui_SliderSolderSet, set_value_event_cb);
            lv_slider_set_value( ui_SliderSolderSet, app_dev.para.run.solder_temp/10, LV_ANIM_OFF);
            lv_obj_add_event_cb(ui_SliderSolderSet, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
        }
    }
    
    if(para_change_timeout>0) {
        para_change_timeout -= TIMER_PERIOD;
        if(para_change_timeout<=0) {
            xEventGroupSetBits(sys_event, kSysEvent_ParaChange);
            app_dev_para_init();
        }
    }
}


static void select_shortkey_back_event_cb(lv_event_t * e)
{
    uint16_t *value = (uint16_t *)(e->user_data);
    
    para_change_timeout = 2000; 
    app_dev.para.run.solder_temp = *value;
    xEventGroupSetBits(sys_event, kSysEvent_UIParaChange);
}

static void set_value_event_cb(lv_event_t * e)
{
    para_change_timeout = 2000; 
    app_dev.para.run.solder_temp = lv_slider_get_value(ui_SliderSolderSet)*10;
}

void solder_group_select(uint8_t id)
{
    uint8_t i;
    lv_timer_pause(lv_timer);
    switch(id){
    case 0:  
        lv_group_add_obj(g_encoder, ui_SliderSolderSet);
        for(i=0;i!=SHORTKEY_NUM;i++)
            lv_group_add_obj(g_encoder, shortkey_btn[i]);
        lv_timer_set_cb(lv_timer, solder_tab0_refresh);
        break;
    default:
        lv_timer_set_cb(lv_timer, NULL);
        break;
    }
    lv_timer_resume(lv_timer);
}
